package com.scs.application.core.utils;

import com.scs.application.core.dto.WebSocketMsgDTO;
import com.scs.application.core.socket.SpdWebSocketServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
* @Description： websocket工具类
*/
@Component
@Slf4j
public class UtilWebsocket {
    @Autowired
    private SpdWebSocketServerService spdWebSocketServerService;
    private static UtilWebsocket services;
    @PostConstruct
    public void init() {
        services = this;
        services.spdWebSocketServerService = this.spdWebSocketServerService;

    }
    //发送信息
    public static void sendTextAll(WebSocketMsgDTO webSocketMsg) {
        services.spdWebSocketServerService.sendTextAll(webSocketMsg);
    }


    //从缓存获取session
    public static void getCache() {
//        try {
//            SpdWebSocketServerService.livingSessions = (Map<String, WsSession>) CacheUtils.get(CacheUtils.SPD_WEBSOCKET_SESSION, CacheUtils.SPD_WEBSOCKET_SESSION);
//        }catch (Exception e) {
//            log.error("UtilWebsocket.getCache.error",e);
//        }
//
//        if (SpdWebSocketServerService.livingSessions == null) SpdWebSocketServerService.livingSessions = new HashMap<String, WsSession>();
    }

    //将session写到缓存
    public static void setCache() {
//        CacheUtils.remove(CacheUtils.SPD_WEBSOCKET_SESSION, CacheUtils.SPD_WEBSOCKET_SESSION);
//        CacheUtils.put(CacheUtils.SPD_WEBSOCKET_SESSION, CacheUtils.SPD_WEBSOCKET_SESSION,SpdWebSocketServerService.livingSessions);
    }
}
